<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    @include('public.h5_head')
    <meta name="csrf_token" id="token" content="{{ csrf_token() }}">
    <title>订单列表</title>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/commonjs/rem.js"></script>
    <link rel="stylesheet" type="text/css" href="{{ env('CDN_DOMAIN_NAME') }}/css/commoncss/common.css"/>
    <link rel="stylesheet" type="text/css" href="{{ env('CDN_DOMAIN_NAME') }}/css/lists.css?v=1.0.1"/>
</head>
<script src="{{ env('CDN_DOMAIN_NAME') }}/js/commonjs/4.2.5iscroll.js"></script>
<script src="{{ env('CDN_DOMAIN_NAME') }}/js/app.js"></script>
<body>
<script>
    var brand_name=[
            @foreach($brandName as $k=>$v)
        [{{ $k }},'{{ $v }}'],
        @endforeach
    ];
    var title=[
            @foreach($datas['pros'] as $k=>$v)
        [{{ $k }},'{{ $v }}'],
        @endforeach
    ];
    function getProTitle(id) {
        for(var i=0;i<title.length;i++)
        {
            if(title[i][0]==id  ){
                return title[i][1]
            }
        }
    }
    function getbrandname(id) {
        for(var i=0;i<brand_name.length;i++)
        {
            if(brand_name[i][0]==id  ){
                return brand_name[i][1]
            }
        }
    }
    var Arr=[[0,"订单待确认"],[1,"待支付"],[2,"交易关闭"],[-1,"支付失败"],[-2,"订单失效"],[-9,"订单超时"],[5,"已确认收货"],[-3,"已退款"],[3,"待发货"],[4,"待发货"],[6,"待发货"],[7,"待发货"]];
    function getstatus(id) {
        for(var i=0;i<Arr.length;i++)
        {
            if(Arr[i][0]==id  ){
                return Arr[i][1]
            }
        }
    }
    function getBtn(status,id){
        if(status==0 || status==1){
            return '<p class="list_sure" id="'+id+'"><a href="javascript:;" class="surePay" title="6" abc="'+id+'">确认支付</a></p>'
        }else if(status==2){
            return '<p class="list_del" id="'+id+'"><a href="javascript:;" class="del_info">删除订单</a></p>'
        }else{
            return ''
        }
    }
    function getoperation(operation_id,operation_name,id){
        if(operation_id==null){
            return ''
        }else{
            return '<p class="list_sure" id="'+id+'"><span class="list_operator" >操作员：'+operation_name+'</span></p>'
        }
    }
    function toDecimal(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x*100)/100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    }
    function getNname(s){
        if(s.indexOf("-")>-1){
            return  s.split("-")[0];
        }else{
            return  s.substr(0,6);
        }
    }
</script>
<div id="box_list">
    <div class="wrap" id="#wrapper">
        <div class="main_list">
            <div class="seek" onclick="window.location.href='/Business/bus_select'">
                <div class="seek_img">
                    <img class="seek_pic" src="/img/seek.png" alt="">
                    <p class="seek_name">搜索</p>
                </div>
            </div>
            @if(count($datas['infos']) == 0)
                <div class="main_empty">
                    <div class="list_empty">
                        <div class="list_img">
                            <img src="/img/00013.png" alt="" />
                        </div>
                    </div>
                    <p class="list_wrods">目前没有订单</p>
                </div>
            @else
            <div class="other_list">
                @foreach($datas['infos'] as $k => $v)
                <div class="list_product">
                    <div class="list_title">
                        <div class="list_icon">
                            <img src="/img/00004.png" alt="" />
                            <span>{{ $brandName[$v->brand_id] }}</span>
                            <div class="list_success">
                                @if( $v->status == 0)
                                    订单待确认
                                @elseif($v->status == 1)
                                    待支付
                                @elseif(in_array($v->status,[3,6,4,7]))
                                    待发货
                                @elseif($v->status == 2)
                                    交易关闭
                                @elseif($v->status == -1)
                                    支付失败
                                @elseif($v->status == -2)
                                    订单失效
                                @elseif($v->status == -9)
                                    订单超时
                                @elseif($v->status == 5)
                                    已确认收货
                                @elseif($v->status == -3)
                                    已退款
                                @endif
                            </div>
                        </div>
                        <div class="list_code"><span>订单编号：</span>{{ $v->ordernum }}</div>
                    </div>
                    <div class="list_list" onclick="window.location.href='/produc?good_id={{ $v->pro_id }}'">
                        <div class="list_list_l">
                            <img src="{{ env('CDN_DOMAIN_NAME') }}/upload/{{ $v->face }}" alt="" class="list_list_img" />
                        </div>
                        <div class="list_list_r">
                            <div class="list_list_name">
                                <p class="list_list_tit">{{ $datas['pros'][$v->pro_id] }}</p>
                                <p class="list_price">￥<b>{{sprintf('%.2f', $v->total_price/$v->num) }}</b></p>
                            </div>
                            <p class="list_list_words">款号：{{ $v->girard }}</p>
                            <div class="list_list_color">
                                <p class="list_list_hue">颜色：{{ $v->color }}</p>
                                <p class="list_list_post">邮费：￥{{ sprintf("%.2f",$v->postage_estimate) }}</p>
                            </div>
                            <div class="list_list_b">
                                <p class="list_list_postage">数量：{{ $v->num }}手</p>
                                    <p class="list_sum">合计：<i>￥{{sprintf("%.2f", $v->total_price + $v->postage_estimate) }}</i></p>
                            </div>
                        </div>
                    </div>
                    <div class="list_people">
                        <p class="list_compellation">买家姓名：<span class="compellation">{{ $v->addressee_name }}</span></p>
                        @if(in_array($v->status,[0,1]) )
                            <p class="list_sure" id="{{ $v->id }}"><a href="javascript:;" class="surePay" title="6" abc="{{ $v->id }}">确认支付</a></p>
                        @elseif($v->status == 2)
                            <p class="list_del" id="{{ $v->id }}"><a href="javascript:;" class="del_info">删除订单</a></p>
                        @endif
                        @if($v->operation_id)
                            <p class="list_sure" id="{{ $v->id }}"><span class="list_operator" >操作员：{{ $v->operation_name }}</span></p>
                        @endif
                    </div>
                </div>
                @endforeach
            </div>
            {{--加载--}}
            <div class="more" abc="">
                <div class="more_center">
                    <i class="pull_icon"></i><span>上拉加载...</span>
                </div>
            </div>
            @endif
        </div>
    </div>
    <div class="delDiv" style="display:none;">
        <div class="delDiv_tip">是否确认？</div>
        <div class="delDiv_info">
            <span id="" class="delDiv_info_left">
                <input id="btnCancel" type="button" value="取 消" class="btn-green btnCancel" />
            </span>
            <span id="" class="delDiv_info_right">
                <input id="btnDelTrue" type="button" value="确 认" class="btn-green btnDelTrue" />
            </span>
        </div>
    </div>
    <div class="list_hide" style="display:none;">
        <p class="list_pass"></p>
        <p class="list_ok">确定</p>
    </div>
    <!--隐藏的删除弹框 end-->
    <div class="expurgate" style="display: none;">操作成功</div>
    <div class="mask" style="display:none;"></div>
</div>
</body>
<script>
    @if(isset($_GET['brand']))
        var brand = '{{ $_GET['brand'] }}';
    @else
        var brand = '';
    @endif

    @if(isset($_GET['girard']))
        var girard = '{{ $_GET['girard'] }}';
    @else
        var girard = '';
    @endif

    @if(isset($_GET['start_time']))
        var start_time = '{{ $_GET['start_time'] }}';
    @else
        var start_time = '';
    @endif

    @if(isset($_GET['end_time']))
        var end_time = '{{ $_GET['end_time'] }}';
    @else
        var end_time = '';
    @endif

    @if(isset($_GET['addressee_name']))
        var addressee_name = '{{ $_GET['addressee_name'] }}';
    @else
        var addressee_name = '';
    @endif

    var kp = '{{ $datas['skip'] }}';
    $(".more").attr("abc",kp);
    /*滚动*/
    var myscroll = new iScroll("#wrapper",{
        onScrollMove:function(){
            if (this.y<(this.maxScrollY)) {
                $('.pull_icon').addClass('flip');
                $('.pull_icon').removeClass('loading');
                $('.more span').text('释放加载...');
            }else{
                $('.pull_icon').removeClass('flip loading');
                $('.more span').text('上拉加载...')
            }
        },
        onScrollEnd:function(){
            if ($('.pull_icon').hasClass('flip')) {
                $('.pull_icon').addClass('loading');
                $('.more span').text('加载中...');
                pullUpAction();
            }

        },
        onRefresh:function(){
            $('.more').removeClass('flip');
            $('.more span').text('上拉加载...');
        }
    });
    function pullUpAction(){
        var skip=$(".more").attr("abc");
        $.ajax({
            url: '/business/apiGetOrders',
            type: 'post',
            headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
            data: {
                skip: skip,
                brand:brand,
                girard:girard,
                start_time:start_time,
                end_time:end_time,
                addressee_name:addressee_name

            },
            dataType: 'json',
            success: function (data) {
                if(data.success){
                    $(".more").attr("abc",data.info.skip);
                    var obj = data.info.infos;
                    if(obj.length > 0){
                        var arr=data.info.infos;
                        var path = '{{ env('CDN_DOMAIN_NAME') }}';
                        for(var i=0; i<arr.length; i++){
                            var html='<div class="list_product">'+
                                        '<div class="list_title">'+
                                            '<div class="list_icon">'+
                                                '<img src="/img/00004.png" alt="" />'+
                                                '<span>'+getbrandname(arr[i].brand_id)+'</span>'+
                                                '<div class="list_success">'+getstatus(arr[i].status)+'</div>'+
                                            '</div>'+
                                            '<div class="list_code"><span>订单编号：</span>'+arr[i].ordernum+'</div>'+
                                        '</div>'+
                                        '<div class="list_list" onclick=window.location.href="/produc?good_id='+arr[i].pro_id+'">'+
                                            '<div class="list_list_l">'+
                                                '<img src="'+path+'/upload/'+arr[i].face+'" alt="" class="list_list_img" />'+
                                            '</div>'+
                                            '<div class="list_list_r">'+
                                                '<div class="list_list_name">'+
                                                    '<p class="list_list_tit">'+getProTitle(arr[i].pro_id)+'</p>'+
                                                    '<p class="list_price">￥<b>'+toDecimal((arr[i].total_price)/(arr[i].num))+'</b></p>'+
                                                '</div>'+
                                                '<p class="list_list_words">款号：'+arr[i].girard+'</p>'+
                                                '<div class="list_list_color">'+
                                                    '<p class="list_list_hue">颜色：'+arr[i].color+'</p>'+
                                                    '<p class="list_list_post">邮费：￥'+toDecimal(arr[i].postage_estimate)+'</p>'+
                                                '</div>'+
                                                '<div class="list_list_b">'+
                                                    '<p class="list_list_postage">数量：'+arr[i].num+'手</p>'+
                                                    '<p class="list_sum">合计：<i>￥'+toDecimal(((Number(arr[i].total_price))+(Number(arr[i].postage_estimate))))+'</i></p>'+
                                                '</div>'+
                                            '</div>'+
                                        '</div>'+
                                        '<div class="list_people">'+
                                            '<p class="list_compellation">买家姓名：<span class="compellation">'+getNname(arr[i].addressee_name)+'</span></p>'+
                                                getBtn(arr[i].status,arr[i].id)+getoperation(arr[i].operation_id,arr[i].operation_name,arr[i].id)+
                                        '</div>'+
                                    '</div>'
                            $('.other_list').append(html)
                        }
                    }else{
                        $(".more_center").empty();
                        $(".more_center").text("我是有底线的");
                    }
                    myscroll.refresh();
                }else{

                }
            }
        });
    }
    if ($('.main_list').height()<$('.wrap').height()) {
        $('.more').hide();
        myscroll.destroy();
    }
    /*客户姓名*/
    $(".compellation").each(function(){
        var s= $(this).text();
        if(s.indexOf("-")>-1){
            s=s.split("-")[0];
        }else{
            s=s.substr(0,6);
        }
        $(this).text(s);
    })
    /*确认支付*/
    $(document).on("click",".surePay",function(){
//    $(".surePay").off().on("click",function(){
        $(".delDiv").show();
        $(".mask").show();
        var status=$(this).attr("title");
        var order_id = $(this).attr('abc');
        $(".btnDelTrue").off().on('click',function() {
            $.ajax({
                url: '/Business/setOrderStatus',
                type: 'POST',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                data: {order_id: order_id, status: status},
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    $(".delDiv").hide();
                    $(".mask").hide();
                    if(data.success == true){
                        $("#"+order_id).parent().parent().children(".list_title").children(".list_icon").children(".list_success").text("待发货");
                        $("#"+order_id).empty();
                        $("#"+order_id).append("<span class='list_operator'>操作员："+data.msg+"</span>");
                        $(".expurgate").show(0).delay(1000).hide(0);

                    }else{
                        $(".list_pass").html("该订单以线下支付");
                        $(".list_hide").show();
                        $(".mask").show();
                    }

                }
            })
        })
    });
    $(".btnCancel").click(function(){
        $(".delDiv").hide();
        $(".mask").hide();
    });
    $(".list_ok").click(function(){
        $(".list_hide").hide();
        $(".mask").hide();
//        window.location.reload();
    });
    $(document).on("click",".skip_a",function(){
        var id=$(this).parent().parent().attr("id");
        window.location.href='/Business/orderInfos/'+id;
    });
    /*删除订单*/
    $(document).on("click",".del_info",function(){
//    $(".del_info").off().on("click",function(){
        $(".delDiv").show();
        $(".mask").show();
        var del_id = $(this).parent().attr('id');
        $(".btnDelTrue").off().on('click',function() {
            $.ajax({
                url: '/Business/delete_order',
                type: 'post',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                data: {id: del_id},
                dataType: 'json',
                success: function (data) {
                    $(".delDiv").hide();
                    $(".mask").hide();
                    if(data.success){
                        $("#"+del_id).parent().parent().remove();
                        if ($('.main_list').height()<$('.wrap').height()) {
                            $('.more').hide();
                            myscroll.destroy();
                        }
                        $(".expurgate").show(0).delay(1000).hide(0);
                    }else{
                        $(".expurgate").text(data.msg);
                        $(".expurgate").show(0).delay(1000).hide(0);
                    }
                }
            })
        })
    });
</script>
</html>
